<?php

namespace app\api\controller;

use app\api\service\BookService;
use http\Client\Request;

/**
 * 图书
 * User: GXA
 * Date: 2022/4/26
 * Time: 9:56
 */
class Book extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    /**
     * @var $service BookService
     */
    protected $service = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->service = new BookService();
    }

    public function lists(Request $request)
    {
        $param = $request->param();
        $this->validate($param, get_class(), 'lists');
        $this->serviceResponse($this->service->lists($param));
    }

    public function detail(Request $request)
    {
        $param = $request->param();
        $this->validate($param, get_class(), 'detail');
        $this->serviceResponse($this->service->detail($param));
    }

    public function packagelist(Request $request)
    {
        $param = $request->param();
        $this->validate($param, get_class(), 'packagelist');
        $this->serviceResponse($this->service->packagelist($param));
    }

    public function publish(Request $request, $id)
    {
        $type = $request->input('type', 1);
        $price = $request->input('price', '0.00');
        $course_directory_ids = $request->input('cd_ids', []);

        $course = Book::find($id);
        $course->type = $type;
        $course->price = $price;

        $result = $course->courseDirectory()->whereIn('id', $course_directory_ids)->update(['is_have_fee' => 0]);

        if (!$result)
        {
            return $this->error('更新失败');
        }

        return $this->success('更新成功');
    }

}